#include <stdio.h>
#include <stdlib.h>

#define SIZE_ARRAY(a)   (sizeof(a)/sizeof(a[0]))

/* find a number in array, it biger than all the left number,
 * buf smaller than all the right number*/
void get_middle(int *a, int size)
{
    int *max, *min, m, n, i;
    char *equal;

    if (a == NULL || size <= 0)
        return ;

    max = malloc(size * sizeof(int));
    min = malloc(size * sizeof(int));
    equal = calloc(sizeof(char), size);
    
    for (m = 0, n = size - 1; m < size, n > -1; m++, n--)
    {
        if (m == 0 && n == size-1)
        {
            max[m] = a[m];
            min[n] = a[n];
            continue;
        }

        if (a[m] > max[m-1])
            max[m] = a[m];
        else
        {
            if (a[m] = max[m-1])
                equal[m] = 1;
            max[m] = max[m-1];
        }

        if (a[n] < min[n+1])
            min[n] = a[n];
        else
        {
            if (a[n] = min[n+1])
                equal[n] = 1;
            min[n] = min[n+1];
        }
    }

    for (i = 0; i < size; i++)
        if (max[i] == min[i] && equal[i] != 1)
            printf("%d ", a[i]);
    printf("\n");

    free(max);
    free(min);
    free(equal);
}


int main(int argc, char const* argv[])
{
    int array1[] = {5, 1, 2, 6, 9, 8};
    int array2[] = {5, 7, 8, 9, 19, 28};
    int array3[] = {5, 7, 8, 9, 7, 28};
    int array4[] = {2};
    int array5[] = {2,1};
    
    get_middle(array1, SIZE_ARRAY(array1));
    get_middle(array2, SIZE_ARRAY(array2));
    get_middle(array3, SIZE_ARRAY(array3));
    get_middle(array4, SIZE_ARRAY(array4));
    get_middle(array5, SIZE_ARRAY(array5));
    
    return 0;
}
